package com.yunsuo;

import com.alibaba.fastjson.JSON;
import com.yunsuo.bean.FingerPrintRaspberry;
import com.yunsuo.utils.HttpPostUtils;

import java.util.concurrent.*;

/**
 * 添加指纹线程
 * 调用python文件并且得到最后的指纹信息
 */
public class AddFingerThread {
    public static void request(){
        ExecutorService executor = Executors.newCachedThreadPool();
        AddFinger addFinger = new AddFinger();
        Future<String> result = executor.submit(addFinger);
        executor.shutdown();

//        //模拟延时
//        try {
//            Thread.sleep(1000);
//        } catch (InterruptedException e1) {
//            e1.printStackTrace();
//        }

        //System.out.println("addFingerThread");

        try {
            String response=result.get();
            //get方法会阻塞直到任务返回结果
            //System.out.println("运行结果:  "+response);
            //System.out.println("1");
            //根据result的返回值开启新线程执行任务
            //JSONObject jsonObject = JSON.parseObject(response);

            //把指纹信息整理为json对象字符串
            //System.out.println("2");
            //System.out.println("指纹获取完毕:"+Thread.currentThread().getName());

            //假装得到指纹信息json字符串
            FingerPrintRaspberry fingerPrint=new FingerPrintRaspberry();
            fingerPrint.setfId(123);
            fingerPrint.setfContentPath("/User/leo/fingerprint");
            //fingerPrint.setfHardwareLocation("8");
            fingerPrint.setfLockId(1);
            fingerPrint.setfName("songmingyu");
            fingerPrint.setfUserId(1);
            //System.out.println("3");
            //Thread.sleep(1000);

            String jsonString=JSON.toJSONString(fingerPrint);
            //System.out.println("树莓派得到的指纹数据=  "+jsonString);


            //发送指纹数据到服务器
            String result1=HttpPostUtils.doPostRequest("addFinger",jsonString);


            //等待服务器返回响应信息
            //System.out.println("服务器响应=   "+result1);



        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        //System.out.println("所有任务执行完毕");
    }
}

class AddFinger implements Callable<String> {
    @Override
    public String call() throws Exception {
        String result="call()方法结束";
        //Thread.sleep(5000);
        //System.out.println("调用python代码");
        //System.out.println("返回指纹数据包");
        //System.out.println("返回指纹在硬件中的位置");
        //System.out.println("调用python线程"+Thread.currentThread().getName());
        return result;
    }
}
